<!--
 * @Author: your name
 * @Date: 2022-03-12 23:18:54
 * @LastEditTime: 2022-03-12 23:20:16
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \算法测试\day0906\Json转换文件.html
-->
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        function download(url, name) {
            const a = document.createElement("a");
            a.download = name;
            a.rel = "noopener";
            a.href = url;
            // 触发模拟点击
            a.dispatchEvent(new MouseEvent("click"));
            // 或者 a.click()
        }
        const json = {
            a: 3,
            b: 4,
            c: 5,
        };
        const str = JSON.stringify(json, null, 2);

        // 方案一：Text -> DataURL
        const dataUrl = `data:,${str}`;
        download(dataUrl, "demo.json");
    </script>
</body>

</html>